﻿// <copyright file="BFProjectDefinesParser.cs" company="Horiba Medical">
//     Copyright (c) Horiba Medical. All rights reserved.
// </copyright>
// <summary>
//     Provides an project defines parser for BF project files.
// </summary>

namespace DoxyOutParser.CodeCheck
{
	using System;
	using System.Xml;
	using System.Xml.XPath;

	/// <summary>
	/// Parses project defines from a BF project file.
	/// </summary>
	public class BFProjectDefinesParser : IProjectDefinesParser
	{
		/// <summary>
		/// The file which will be parsed.
		/// </summary>
		private string blackfinProjectFile;

		/// <summary>
		/// Initializes a new instance of the BFProjectDefinesParser class.
		/// </summary>
		/// <param name="projectFile">The file to parse.</param>
		public BFProjectDefinesParser(string projectFile)
		{
			// TODO Check the value of string and eventually check if exist or not.
			this.blackfinProjectFile = projectFile;
		}

		#region IProjectDefinesParser Members

		/// <summary>
		/// Returns a list of every project defines specified in a BF project file.
		/// </summary>
		/// <returns>The project define array.</returns>
		public string[] GetProjectDefinesTable()
		{
			// Using Xpath Document
			XPathDocument blackfinProject = new XPathDocument(this.blackfinProjectFile);
			XPathNavigator blackfinProjectNavigator = blackfinProject.CreateNavigator();

			string compilerOptionRaw = (string)blackfinProjectNavigator.Evaluate("string(/visualdsp-project/configurations/configuration[@name=\"Debug\"]/tools/tool[@type=\"Compiler\"]/option)");

			if (!compilerOptionRaw.Equals(string.Empty))
			{
				int startIndex = compilerOptionRaw.IndexOf("|-D>") + "|-D>".Length;
				int endIndex = compilerOptionRaw.IndexOf("|", startIndex);
				string includeDirsData = compilerOptionRaw.Substring(startIndex, endIndex - startIndex);
				string[] dirsSeparators = { ";" };
				return includeDirsData.Split(dirsSeparators, StringSplitOptions.RemoveEmptyEntries);
			}
			else
			{
				return null;
			}
		}

		#endregion

		/// <summary>
		/// Gets the full path to an XML node.
		/// </summary>
		/// <param name="xmlNode">The XML node to parse.</param>
		/// <returns>The full path.</returns>
		private string GetFullXPath(XmlNode xmlNode)
		{
			string newFullPath = this.GetParentBrowsePath(xmlNode, xmlNode.Name);

			return newFullPath;
		}

		/// <summary>
		/// Gets the path to an XML node's parent.
		/// </summary>
		/// <param name="xmlNode">The XML node to parse.</param>
		/// <param name="xmlNodePath">The path to this node.</param>
		/// <returns>The path to the parent node.</returns>
		private string GetParentBrowsePath(XmlNode xmlNode, string xmlNodePath)
		{
			string xmlNewNodePath;
			if (xmlNode.ParentNode != null)
			{
				xmlNewNodePath = this.GetParentBrowsePath(xmlNode.ParentNode, xmlNode.ParentNode.Name + "/" + xmlNodePath);
			}
			else
			{
				xmlNewNodePath = xmlNodePath;
			}

			return xmlNewNodePath;
		}
	}
}
